using System;
using System.Collections.Specialized;
using System.Web;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Microsoft.Practices.EnterpriseLibrary.ExceptionHandling;
using Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Configuration;
using Project.Practice.MVP.Framework;

namespace Project.Practice.MVP.ExceptionHandling
{
    /// <summary>
    /// This is binded in web.config, see detail by "right click 'web.config' --> edit enterprise library configuration"
    /// 
    /// This will deal with "Global Policy"
    /// 
        //<add name="Global Policy">
        //    <exceptionTypes>
        //      <add type="System.Exception, mscorlib, Version=2.0.0.0, Culture=neutral,&#xD;&#xA;						PublicKeyToken=b77a5c561934e089"
        //        postHandlingAction="None" name="Exception">
        //        <exceptionHandlers>
        //          <add type="Project.Practice.MVP.ExceptionHandling.ApplicationExceptionHandler, Project.Practice.MVP.ExceptionHandling, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"
        //            name="Custom Handler" />
        //        </exceptionHandlers>
        //      </add>
        //    </exceptionTypes>
        //  </add>
    /// </summary>
    [ConfigurationElementType(typeof(CustomHandlerData))]
    public class ApplicationExceptionHandler : IExceptionHandler
    {
        private const string UNEXPECTED_ERROR = "Unexpected Error!!";
        public ApplicationExceptionHandler(NameValueCollection ignore)
        {

        }

        #region IExceptionHandler Members
        public Exception HandleException(Exception exception, Guid correlationID)
        {
            try
            {
                /* This is Critical Layer Exception.
                For E.g Database Unavailable
                Web Service Unavailable and so on .*/               
                if (exception.GetType().Equals(typeof(CustomException.CriticalException)))
                {
                    if (HttpContext.Current.Items.Contains("ERROR_MSG"))
                    {
                        HttpContext.Current.Items.Remove("ERROR_MSG");
                    }
                    HttpContext.Current.Items.Add("ERROR_MSG", exception.Message);
                    HttpContext.Current.Server.Transfer("~/CriticalException.aspx", false);
                }    
                //This is Presenter Layer Exception.
                else if (exception.GetType().Equals(typeof(CustomException.PresenterLayerException)))
                {

                    if ((CustomPage)HttpContext.Current.Handler != null)
                    {
                        ((CustomPage)HttpContext.Current.Handler).Error = "Presenter Layer Exception :" + exception.InnerException.Message ;                                         
                    }
                }
                //This is View Layer Exception.
                else if (exception.GetType().Equals(typeof(CustomException.ViewLayerException)))
                {
                    if ((CustomPage)HttpContext.Current.Handler != null)
                    {
                        ((CustomPage)HttpContext.Current.Handler).Error ="View Layer Exception:" + exception.Message;                     
                     }
                }
                //This is DataAccess Layer Exception
                else if (exception.GetType().Equals(typeof(CustomException.DataAccesLayerException)))
                {
                    if ((CustomPage)HttpContext.Current.Handler != null)
                    {                      
                        ((CustomPage)HttpContext.Current.Handler).Error = "DataAccess Layer Exception: "+ exception.Message;
                    }
                }
                //This is SQL DAL Layer Exception
                else if (exception.GetType().Equals(typeof(CustomException.SQLDALException)))
                {
                    if ((CustomPage)HttpContext.Current.Handler != null)
                    {
                        ((CustomPage)HttpContext.Current.Handler).Error = "SQL Error 101: " + exception.Message;
                    }
                }
                else
                {
                    if ((CustomPage)HttpContext.Current.Handler != null)
                    {
                       //This is Session Expired Exception
                        if ((((CustomPage)HttpContext.Current.Handler).CurrentSession == null))
                        {
                            HttpContext.Current.Items["ERROR_MSG"] = "Access Denied";
                            HttpContext.Current.Server.Transfer("~/CriticalException.aspx", false);
                        }
                        //This is General Exception:UN_DEFINED EXCEPTION
                        HttpContext.Current.Server.Transfer("~/CriticalException.aspx", false);
                    }
                }
            }
            catch (System.Threading.ThreadAbortException ex)
            {
            }
            catch (Exception ex)
            {
            }
            return exception;
        }

        #endregion
    }
}
